package com.woniu.microuserserver.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.RolePermEntity;
import com.woniu.microcommonsserver.entity.UserInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microcommonsserver.util.JWTUtil;
import com.woniu.microuserserver.bean.PremsOfRole;
import com.woniu.microuserserver.bean.ResultMsg;
import com.woniu.microuserserver.bean.SearchQuery;
import com.woniu.microuserserver.bean.UserBean;
import com.woniu.microuserserver.feignApi.ProFeignApi;
import com.woniu.microuserserver.service.RoleService;
import com.woniu.microuserserver.service.UserService;
import com.woniu.microuserserver.utils.MyArrayUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @RequestMapping("/ishasname")
    public ResultMsg testUserName(String userName){
        System.out.println(userName);
        ResultMsg msg = new ResultMsg();
        if(userName.equals("汪广")){
            msg.setCode(200);
            msg.setMessage("该用户存在");
            return msg;
        }
        msg.setCode(10000);
        msg.setMessage("该用户不存在");
        return msg;
    }

    //用户登录
    @RequestMapping("/login")
    public ResultMsg login(@RequestBody UserBean user){
        System.out.println(user);
        //数据库校验
        ResultMsg msg = new ResultMsg();
        if(user.getUserName().equals("UserWangWang") && user.getPassWord().equals("123456")){
            msg.setCode(200);
            msg.setMessage("登录成功");
            //如果认证通过了，就直接产生JWT令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put("userName",user.getUserName());
            String token = JWTUtil.createToken(claims);
            System.out.println(token);
            msg.setData(token);
            return msg;
        }
        msg.setCode(10000);
        msg.setMessage("该用户不存在");
        return msg;
    }

    //查询用户
    @RequestMapping("/search")
    public ResponseMsg searchUser(@RequestBody SearchQuery searchQuery){
        PageInfo<UserInfoEntity> pageInfo = userService.searchByquery(searchQuery);
        List<UserInfoEntity> list = pageInfo.getList();
        ResponseMsg msg = new ResponseMsg().builder().code(200).msg("查询成功").data(pageInfo).build();
        return msg;
    }

    //新增用户
    @RequestMapping("/add")
    public ResponseMsg addUser(@RequestBody UserInfoEntity userInfoEntity){
        int i = userService.addUser(userInfoEntity);
        ResponseMsg msg = new ResponseMsg();
        if(i!=0){
            msg.setCode(200);
            msg.setMsg("新增成功");
            return msg;
        }
        msg.setCode(10000);
        msg.setMsg("新增失败");
        return msg;
    }

    //查询一个员工详情
    @RequestMapping("/searchone")
    public ResponseMsg searchOnebyLoginName(String loginName){
        UserInfoEntity userInfoEntity = userService.searchOneByname(loginName);
        ResponseMsg msg = ResponseMsg.builder().code(200).msg("查询成功").data(userInfoEntity).build();
        return msg;
    }

    //查询一个员工详情根据id
    @RequestMapping("/searchByUid")
    public ResponseMsg searchOnebyUid(int uid){
        UserInfoEntity userInfoEntity = userService.searchByUid(uid);
        ResponseMsg msg = ResponseMsg.builder().code(200).msg("查询成功").data(userInfoEntity).build();
        return msg;
    }

    //修改一个员工
    @RequestMapping("/update")
    public ResponseMsg changeUser(@RequestBody UserInfoEntity userInfoEntity){
        int i = userService.changeUser(userInfoEntity);
        ResponseMsg msg = new ResponseMsg();
        if (i!=0){
            msg.setMsg("修改成功");
            msg.setCode(200);
            return msg;
        }
        msg.setMsg("修改失败");
        msg.setCode(10000);
        return msg;
    }

    //删除一个员工
    @RequestMapping("/del")
    public ResponseMsg delUser(String loginName){
        ResponseMsg msg = new ResponseMsg();
        int i = userService.delUser(loginName);
        if (i!=0){
            msg.setMsg("删除成功");
            msg.setCode(200);
            return msg;
        }
        msg.setMsg("删除失败");
        msg.setCode(10000);
        return msg;
    }

    //禁用一条用户
    @RequestMapping("/stop")
    public ResponseMsg stopUser(String loginName,String status){
        ResponseMsg msg = new ResponseMsg();
        int i = userService.stopUser(loginName, status);
        if (i!=0){
            msg.setMsg("修改成功");
            msg.setCode(200);
            return msg;
        }
        msg.setMsg("修改失败");
        msg.setCode(10000);
        return msg;
    }

    //重置密码
    @RequestMapping("/resetpd")
    public ResponseMsg resetpd(String loginName){
        ResponseMsg msg = new ResponseMsg();
        int i = userService.resetpd(loginName);
        if (i!=0){
            msg.setMsg("修改成功");
            msg.setCode(200);
            return msg;
        }
        msg.setMsg("修改失败");
        msg.setCode(10000);
        return msg;
    }

    //根据ids查询员工信息
    @RequestMapping("/searchbyids")
    public ResponseMsg searchbyids(@RequestBody int[] ids){
        List<UserInfoEntity> list = userService.searchbyids(ids);
        return ResponseMsg.builder().code(200).msg("调用成功").data(list).build();
    }

    //查询所有部门
    @RequestMapping("/searchDep")
    public ResponseMsg searchDep(){
        List<String> list = userService.searchdepname();
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }

    //给廖健的
    @RequestMapping("/searchfour")
    public ResponseMsg findFour(){
        List<UserInfoEntity> searchfour = userService.searchfour();
        ResponseMsg msg = ResponseMsg.builder().msg("调用成功").code(200).data(searchfour).build();
        return msg;
    }

    //根据用户主键查询用户权限
    @RequestMapping("/searchPermsByUid")
    public ResponseMsg searchPermsByUid(int uid){
        //先查询uid对应的角色主键uid5 对应rid2,5,[41,124,2,24,]
        List<Integer> rids = userService.searchRidsByUid(uid);
        //查询每个角色对应的权限数据
        TreeSet hset = new TreeSet();
        TreeSet wset = new TreeSet();
        TreeSet bset = new TreeSet();
        TreeSet uset = new TreeSet();
        for (int i = 0; i < rids.size() ; i++) {
            RolePermEntity rolePermEntity = roleService.getPermsByRid(rids.get(i));
            //转化为集合类型的perms
            PremsOfRole premOfRole = MyArrayUtils.getPremOfRole(rolePermEntity);
            List<Integer> hps = premOfRole.getHps();
            hset.addAll(hps);
            List<Integer> wps = premOfRole.getWps();
            wset.addAll(wps);
            List<Integer> bps = premOfRole.getBps();
            bset.addAll(bps);
            List<Integer> ups = premOfRole.getUps();
            uset.addAll(ups);
        }
        PremsOfRole prems = new PremsOfRole();
        prems.setHps(new ArrayList(hset));
        prems.setWps(new ArrayList(wset));
        prems.setBps(new ArrayList(bset));
        prems.setUps(new ArrayList(uset));
        ResponseMsg msg = ResponseMsg.builder().code(200).msg("查询成功").data(prems).build();
        return msg;
    }

}
